package client

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client" // 修改为正确的导入路径
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var OrdersGroup = core.Group("/client", newOrdersHandler, regOrders, middleware.TokenAuth())

func newOrdersHandler(srv client.IOrdersService) *ordersHandler { // 添加包前缀
	return &ordersHandler{srv: srv}
}

func regOrders(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *ordersHandler) {
		rg.GET("/orders/list", handle.list)
		rg.GET("/orders/detail", handle.detail)
		rg.GET("/orders/call_back", handle.callBack)
		rg.GET("/orders/statistic", handle.statistic)
		rg.GET("/orders/ad_data", handle.adData)
		rg.GET("/orders/ad_data2", handle.adData)
	})
}

type ordersHandler struct {
	srv client.IOrdersService // 添加包前缀
}

// list orders列表
func (hd ordersHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.OrdersListReq // 添加包前缀
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(c, page, listReq) // 修正变量名为 hd
	response.CheckAndRespWithData(c, res, err)
}

// detail orders详情
func (hd ordersHandler) detail(c *gin.Context) {
	var detailReq req.OrdersDetailReq // 添加包前缀
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.ID)
	response.CheckAndRespWithData(c, res, err)
}

// edit orders编辑
func (hd ordersHandler) callBack(c *gin.Context) {
	var orderReq req.OrdersDetailReq // 添加包前缀
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &orderReq)) {
		return
	}
	res, err := hd.srv.CallBack(orderReq.ID)
	response.CheckAndRespWithData(c, res, err)
}

// statistic
func (hd ordersHandler) statistic(c *gin.Context) {

	var listReq req.OrdersListReq

	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &listReq)) {
		return
	}

	res, err := hd.srv.Statistic(c, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// adData
func (hd ordersHandler) adData(c *gin.Context) {
	var adDataReq req.AdDataReq // 添加包前缀
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &adDataReq)) {
		return
	}
	res, err := hd.srv.AdData(c, adDataReq) // 修正变量名为 hd
	response.CheckAndRespWithData(c, res, err)
}
